﻿Imports System.Data.SqlClient
Imports System.Globalization
Imports System.Data
Imports System.Linq
Imports CodeMatrix.Context
Imports System.Collections.Generic

Partial Class Apps_MasterPage
    Inherits System.Web.UI.MasterPage

    ''' <summary>
    ''' By pass operations!!!
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DoByPass()
        Try
            ' =========================================
            ' Sign out with by pass parameter!!!
            ' =========================================
            If Not String.IsNullOrEmpty(HttpContext.Current.Request.QueryString("b_signout")) Then
                ' Comment by Boonyarit
                'FormsAuthentication.SignOut()
                Response.Redirect("~/default.aspx")
            End If

            ' =========================================
            ' Unlocked user!!!
            ' =========================================
            If Not String.IsNullOrEmpty(HttpContext.Current.Request.QueryString("b_unlocked_user")) Then
                Membership.GetUser(HttpContext.Current.Request.QueryString("b_user")).UnlockUser()
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Page.Title = "GHBank System"
            If HttpContext.Current.Session("session_id") Is Nothing Then
                Response.Redirect("Logout.aspx")
            Else
                If CodeMatrix.Business.GenericData.IsOnline() = "" Then
                    Response.Redirect("Logout.aspx")
                Else
                    loginname.Text = String.Format("คุณ {0}", HttpContext.Current.Session("first_name") & " " & HttpContext.Current.Session("last_name"))
                    If Not IsPostBack Then
                        Me.InitCtrl()
                        Me.SetMenu(Menu1.Items)
                        'Me.DoByPass()
                    End If
                End If
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Private Sub SetMenu(ByVal mnu As MenuItemCollection)
        Try
            CodeMatrix.Business.GenericData.GetMenu(mnu)
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    ' Modified by Boonyarit Yingvoragan
    ' Modified date 20/10/2553
    ' Not used
    Private Function GetUrl() As String

        Dim url As String = String.Empty

        For Each i As String In HttpContext.Current.Request.Url.Segments.Skip(3)
            url &= i
        Next

        If Request.QueryString.Count <> 0 Then
            If String.IsNullOrEmpty(Request.QueryString("readonly")) Then
                url &= "?" & Request.QueryString.GetKey(0)
            End If
        End If

        url = url.Replace("_fv.aspx", "_list.aspx")

        Return url

    End Function

    Private Shared Sub GetMenuMain(ByVal rootMenu As MenuItemCollection, ByVal tbl As DataTable)

        Dim cMenuSystemId As List(Of Int32) = New List(Of Int32)()
        Dim itemSystemMenu As MenuItem = New MenuItem()
        itemSystemMenu.Text = "ข้อมูลหลักของระบบ"
        rootMenu.Add(itemSystemMenu)
        Dim cMenuSystem = tbl.AsEnumerable() _
                          .Where(Function(f) 1 = 1).GroupBy(Function(f) New With {.menu_system_code = f.Field(Of Int32)("menu_system_code"), _
                          .menu_system_name = f.Field(Of String)("menu_system_name")})

        For Each i In cMenuSystem
            Dim iMenu As MenuItem = New MenuItem() With {.Text = i.Key.menu_system_name, .Value = i.Key.menu_system_code}

            If Not cMenuSystemId.Contains(i.Key.menu_system_code) Then

                cMenuSystemId.Add(i.Key.menu_system_code)
                itemSystemMenu.ChildItems.Add(iMenu)

                ' ========================================
                ' Get menu for sub menu
                ' ======================================== 
                For Each j In tbl.AsEnumerable() _
                    .Where(Function(f) f.Field(Of Int32)("menu_system_code") = i.Key.menu_system_code) _
                    .GroupBy(Function(f) New With {.sub_menu_code = f.Field(Of Int32)("sub_menu_code"), _
                                                   .type_name = f.Field(Of System.String)("type_name"), _
                                                   .sub_menu_url = f.Field(Of System.String)("sub_menu_url"), _
                                                   .menu_system_code = f.Field(Of System.Int32)("menu_system_code")})

                    Dim jMenu As MenuItem = New MenuItem() With {.Text = j.Key.type_name, _
                                                                 .Value = j.Key.sub_menu_code, _
                                                                 .NavigateUrl = j.Key.sub_menu_url}
                    iMenu.ChildItems.Add(jMenu)
                Next
            End If
        Next

    End Sub

    Private Sub InitCtrl()

        Try
            Dim nowDate As String = DateTime.Now.ToString("dd MMMM yyyy", New CultureInfo("th-TH"))

            ' Commented by Boonyarit Yingvoragan
            ' Commented date 14/11/2553
            'Dim dueTime As Int32 = CodeMatrix.Utilities.CodeHelper.GetSingleObject("exec USP_GetUserShowTime @UserId;", _
            '                                                                   New SqlParameter() { _
            '                                                                       New SqlParameter("@UserId", CType(Membership.GetUser().ProviderUserKey, Guid))})
            'ltrScriptSession.Text = String.Format("<script type='text/javascript'>updateSessionId = setInterval('updateSession(""{0}"");', 1000 * 60 * 10);</script>", _
            '                                      Membership.GetUser().ProviderUserKey)

            If HttpContext.Current.Session("staff_last_login") Is System.DBNull.Value Then
                lblShowTime.Text = String.Empty
            Else
                lblShowTime.Text = String.Format(lblShowTime.Text, _
                                                 nowDate, HttpContext.Current.Session("staff_last_login"))
            End If

            CodeMatrix.Business.GenericData.SetBreadCrump(tdBreadCrump, ltrBreadCrump, 0)

        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub
End Class

